package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.OrdersMapper;
import com.zretc.dao.RefundMapper;
import com.zretc.entity.Orders;
import com.zretc.entity.Refund;
import com.zretc.service.BackOrdersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BackOrderServiceImpl implements BackOrdersService {
    private static final Logger LOGGER = LoggerFactory.getLogger(BackOrderServiceImpl.class);

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private RefundMapper refundMapper;

    @Override
    public PageInfo<Orders> getOrdersList(Map<String, String> condition) {
        // 获取页码
        String num = condition.get("pageNum");
        if (num == null){
            num = "1";
        }
        Integer pageNum = Integer.valueOf(num);
        // 获取显示数量
        String size = condition.get("pageSize");
        if (size == null){
            size = "10";
        }
        // 判断是否有用户编号
        String accountId = condition.get("accountId");
        if("".equals(accountId)||accountId==null){
            condition.put("accountId",null);
        }
        // 判断电影名称是否为""
        String filmName = condition.get("filmName");
        if("".equals(filmName)){
           condition.put("filmName",null);
        }else if(filmName != null){
            condition.put("filmName","%"+filmName+"%");
        }
        // 判断用户账号是否为""
        String accountName = condition.get("accountName");
        if("".equals(accountName)){
            condition.put("accountName",null);
        }else if(accountName != null){
            condition.put("accountName","%"+accountName+"%");
        }
        // 判断订单编号是否为""
        String ordersId = condition.get("ordersId");
        if("".equals(ordersId)){
            condition.put("ordersId",null);
        }else if(ordersId != null){
            condition.put("ordersId","%"+ordersId+"%");
        }
        // 判断时间是否为空
        String startTime = condition.get("startTime");
        if("".equals(startTime)){
            condition.put("startTime",null);
        }
        String endTime = condition.get("endTime");
        if("".equals(endTime)){
            condition.put("endTime",null);
        }
        // 判断状态
        String ordersStatus = condition.get("ordersStatus");
        if("".equals(ordersStatus)){
            condition.put("ordersStatus",null);
        }
        Integer pageSize = Integer.valueOf(size);
        PageHelper.startPage(pageNum,pageSize);
        List<Orders> list = ordersMapper.getOrdersList(condition);
        PageInfo<Orders> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Refund> getRefundList(Map<String, String> condition) {
//        String refundId,
//        String ordersId,
//        String accountName,
//        String pageSize,
//        String pageNum,
//        String startTime,
//        String endTime,
//        String refundStatus
        // 获取页码
        String num = condition.get("pageNum");
        if (num == null){
            num = "1";
        }
        Integer pageNum = Integer.valueOf(num);
        // 获取显示数量
        String size = condition.get("pageSize");
        if (size == null){
            size = "10";
        }
        Integer pageSize = Integer.valueOf(size);

        String accountId = condition.get("accountId");
        if("".equals(accountId)||accountId==null){
            condition.put("accountId",null);
        }

        String refundId = condition.get("refundId");
        if("".equals(refundId)||refundId==null){
            condition.put("refundId",null);
        }else{
            condition.put("refundId","%"+refundId+"%");
        }String ordersId = condition.get("ordersId");
        if("".equals(ordersId)||ordersId==null){
            condition.put("ordersId",null);
        }else{
            condition.put("ordersId","%"+ordersId+"%");
        }
        String accountName = condition.get("accountName");
        if("".equals(accountName)||accountName==null){
            condition.put("accountName",null);
        }else{
            condition.put("accountName","%"+accountName+"%");
        }
        String startTime = condition.get("startTime");
        if("".equals(startTime)){
            condition.put("startTime",null);
        }
        String endTime = condition.get("endTime");
        if("".equals(endTime)){
            condition.put("endTime",null);
        }
        String refundStatus = condition.get("refundStatus");
        if("".equals(refundStatus)){
            condition.put("refundStatus",null);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Refund> list = refundMapper.getRefundList(condition);
        return new PageInfo<Refund>(list);
    }

    @Override
    @Transactional
    public boolean backOrder(String refundId) {
        try{
            // 根据退单编号查询退单号
            Refund refund = refundMapper.getRefundById(refundId);
            // 修改退单申请表状态
            refundMapper.update(refundId,1);
            // 由订单号获取状态
            Orders oneOrder = ordersMapper.getOneOrder(refund.getOrders().getOrdersId());
            if(oneOrder.getOrdersStatus() == 1){
                // 修改订单状态
                ordersMapper.updateStatus(oneOrder.getOrdersId(),3); // 3为已退票
            }else{
                throw new RuntimeException("订单号异常");
            }


        }catch(Exception e){
            LOGGER.debug(e.getMessage());
            e.printStackTrace();

            return false; // 操作失败
        }
        return true; // 操作成功
    }

    @Override
    public Orders getOrders(String ordersId) {
        return ordersMapper.getOneOrder(ordersId);
    }

    @Override
    public Refund getRefund(String refundId) {
        return refundMapper.getRefundById(refundId);
    }


}
